﻿using System;
using System.Xml.Linq;
using System.Collections.Generic;

namespace SoftwareLogistics.SharepointClient.Library.Models
{
    public class SPUser
    {
        public int ID { get; set; }
        public string Email { get; set; }
        public string Name { get; set; }
        public string UserType { get; set; }
        public List<string> Roles { get; set; }

        public static SPUser Create(XElement element)
        {
            var user = new SPUser()
            {
                ID = Convert.ToInt32((string)element.Attribute("ID")),
                Email = (string)element.Attribute("Email"),
                Name = (string)element.Attribute("Name"),
                UserType = "TBD",
                Roles = new List<string>()
            };

            var parts = user.Name.Split('\\');
            if (parts.Length > 1)
                user.Name = parts[1];

            parts = user.Name.Split('|');
            if (parts.Length > 2)
                user.Name = parts[2];

            return user;
        }

        public override string ToString()
        {
            var parts = Name.Split('\\');
            if (parts.Length > 1)
                return parts[1];

            return Name;
        }
    }
}